package com.whynot.frame.manythread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 描    述:公平锁与非公平锁
 * 创建时间: 2018/12/24
 */
public class MyReturnAndExecutorLock {

    ReentrantLock lock = new ReentrantLock(true);

    static ExecutorService executorService = Executors.newWorkStealingPool();

    public void test(int count){
        try {
            lock.lock();
            System.out.println("当前线程"+Thread.currentThread().getName()+"获得了锁"+count);
        }finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        MyReturnAndExecutorLock returnLock = new MyReturnAndExecutorLock();
        for (int i = 1; i <= 30000000; i++) {
            final int count=i;//子线程里面取常量
            System.out.println("测试："+i);
            System.out.println("测试2："+count);
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    /*if(count > 1000000 && count < 1000200){
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        int i = 1/0;
                        throw new IllegalStateException("陈瑶分店点省公司");
                    }*/
                    System.out.println(Thread.currentThread().getName()+"启动"+count);
                    //returnLock.test(count);
                }
            });
        }
       /* Runnable runnable = () -> {
            System.out.println(Thread.currentThread().getName()+"启动");
            returnLock.test();
        };
        Thread[] threadArray = new Thread[100];
        for (int i=0; i<100; i++) {
            threadArray[i] = new Thread(runnable);
        }
        for (int i=0; i<100; i++) {
            threadArray[i].start();
        }*/
    }

}